/**
 * 
 */

Ext.namespace("Ext.lams");
Ext.lams.parameterFormPanel = Ext.extend(Ext.form.FormPanel, {
			_vtype:'',
			constructor : function(config) {
				Ext.apply(this, config || {});
				// var _isBlank = this.isBlank==null?false:this.isBlank;
				_vtype = this.vt == null ? '' : this.vt;
				Ext.lams.parameterFormPanel.superclass.constructor.call(this, {
							layout : {
								type : 'form',
								align : 'stretch'
							},
							defaults : {
								anchor : "95%"
							},
							defaultType : 'textfield',
							labelWidth : 65,
							baseCls : "x-plain"
						});
			},
			initComponent : function() {
				this.items = [{
							xtype : 'hidden',
							name : 'PARAMETER_ID'
						}, {
							fieldLabel : '参数名称',
							allowBlank : false,
							vtype : this._vtype,
							blankText : "该项为必填项",
							name : "PARAMETER_NAME"
						}, {
							fieldLabel : '参数值',
							allowBlank : false,
							blankText : "该项为必填项",
							name : "PARAMETER_VALUE"
						}, {
							xtype : 'treecombobox',
							fieldLabel : '单位',
							emptyText : '请选择...',
							treeUrl : 'getUnitTree.action',
							rootText : '加载节点',
							rootId : '0'
						}, {
							xtype : 'textarea',
							fieldLabel : '说明',
							name : 'REMARK'
						}];
				Ext.lams.parameterFormPanel.superclass.initComponent.call(this);
			},
			getValue : function() {
				if (this.getForm().isValid())
					return new Ext.data.Record(this.getForm().getValues());
				// alert(this.getForm().getValues());
				else
					throw Error("表单填写出错");
			},
			setValue : function(_r) {
				var node = {
					id : _r.data['ORG_NO'],
					text : _r.data['ORG_NAME']
				}
				this.find('fieldLabel', '单位')[0].setValue(node);
				this.getForm().loadRecord(_r);
			},
			clear : function() {
				this.getForm().reset();
			}
		})
Ext.lams.parameterInfoWindow = Ext.extend(Ext.Window, {
			form : null,
			constructor : function(config) {
				Ext.apply(this, config || {});
				var _vtype = this.vt == null ? '' : this.vt;
				this.form = new Ext.lams.parameterFormPanel({
							'vt' : _vtype
						});
				Ext.lams.parameterInfoWindow.superclass.constructor.call(this,
						{
							plain : true,
							width : 380,
							closeAction : "hide",
							items : this.form,
							modal : true,
							listener : {

							}
						});
				this.addEvents("submit");
			},
			initComponent : function() {
				this.buttons = [{
							// text : "确定",
							xtype : "btn_submit",
							handler : this.onSubmitClick,
							scope : this
						}, {
							// text : "取消",
							xtype : "btn_cancel",
							handler : this.onCancelClick,
							scope : this
						}];
				Ext.lams.parameterInfoWindow.superclass.initComponent.call(this);
			},
			close : function() {
				this.form.clear();
				this.hide();
			},
			onSubmitClick : function() {
				var _win = this;
				try {
					var _r = this.form.getValue();
					var node = _win.form.find('fieldLabel', '单位')[0].getNode()
					_r.data["ORG_NO"] = node.id;
					_r.data["ORG_NAME"] = node.text;

					this.form.form.submit({
								url : 'saveOrUpdateParameter.action',
								method : 'POST',
								waitMsg : '保存数据...',
								params : {
									'ORG_NO' : _win.form.find('fieldLabel',
											'单位')[0].getValue()
								},
								success : function(form, action) {
									_win.fireEvent("submit", this, _r);
									_win.close();
								},
								failure : function(form, action) {
									H.Msg.error(action.result.message);
									// Ext.Msg.alert('failure',
									// action.result.message);
								}
							});
				} catch (e) {
					// Ext.Msg.alert("提示", e.message);
					H.Msg.error(e.message);
				}
			},
			onCancelClick : function() {
				this.close();
			}
		})
AddParameterWindow = Ext.extend(Ext.lams.parameterInfoWindow, {
			title : "添加参数",
			vt : 'checkParameName'
		})
UpdateParameterWin = Ext.extend(Ext.lams.parameterInfoWindow, {
			title : '修改参数',
			load : function(_r) {
				this.form.setValue(_r);
			}
		})
Ext.lams.ParameterManagerPanel = Ext.extend(Ext.grid.GridPanel, {
			addWin : new AddParameterWindow(),
			updateWin : new UpdateParameterWin(),
			constructor : function(config) {
				Ext.apply(this, config || {});
				Ext.lams.ParameterManagerPanel.superclass.constructor.call(
						this, {
							frame : true,
							layout : 'form',
							loadMask : {
								msg : '正在载入数据,请稍等...'
							},
							listeners : {
								dblclick : function() {
									try {
										this.updateWin.show();
										this.updateWin.load(this
												.getSelectedParameter());
									} catch (_err) {
										// Ext.Msg.alert("提示", _err.message);
										H.Msg.error(_err.message)
									}
								}
							}
						});
			},
			initComponent : function() {
				var sm = new Ext.grid.RowSelectionModel({
							singleSelect : true
						});
				var cm = new Ext.grid.ColumnModel([{
							header : '参数编号',
							width : '20',
							hidden : true,
							dataIndex : 'PARAMETER_ID'
						}, {
							header : "参数名称",
							width : '60',
							dataIndex : "PARAMETER_NAME"
						}, {
							header : "参数值",
							width : '60',
							dataIndex : "PARAMETER_VALUE"
						}, {
							header : "参数说明",
							width : '200',
							dataIndex : "REMARK"
						}, {
							header : '所属单位',
							dataIndex : "ORG_NAME"
						}, {
							header : '单位编号',
							hidden : true,
							dataIndex : "ORG_NO"
						}]);
				var tbar = [{
							xtype : 'btn_add',
							// text : "添加",
							// iconCls : 'btn-add',
							id : 'btn_add',
							disabled : Utils.isEnableBtn(this.btns, 'btn_add'),
							width : '75',
							handler : function() {
								this.addWin.show();
							},
							scope : this
						}, "-", {
							xtype : 'btn_edit',
							// text : "修改",
							// id : 'btn_modify',
							// iconCls : 'btn-edit',
							disabled : Utils.isEnableBtn(this.btns,
									'btn_modify'),
							width : '75',
							handler : function() {

								try {
									this.updateWin.load(this
											.getSelectedParameter());
									this.updateWin.show();
								} catch (_err) {
									// Ext.Msg.alert("提示", _err.message);
									H.Msg.error(_err.message, function() {
												this.updateWin.close();
											});
								}
							},
							scope : this
						}, "-", {
							xtype : 'btn_delete',
							// text : "删除",
							// id : 'btn_delete',
							disabled : Utils.isEnableBtn(this.btns,
									'btn_delete'),
							// iconCls : 'btn-delete',
							width : '75',
							handler : function() {
								// Ext.Msg.confirm("提示", "确认删除选中记录吗",
								// this.onRemoveParameter, this);
								H.Msg.confirm("确认删除选中记录吗",
										this.onRemoveParameter);
							},
							scope : this
						}, "-", '单位', " ", {
							xtype : 'treecombobox',
							// fieldLabel : '单位',
							// width:'200',
							emptyText : '请选择...',
							treeUrl : 'getUnitTree.action',
							rootText : '加载节点',
							rootId : '0',
							hiddenName : 'unit.orgNo'

						}, "-", {
							xtype : 'btn_query',
							// id : 'btn_query',
							width : '75',
							// iconCls : 'silk-zoom',
							// text : ' 查询',
							handler : function(btn) {
								btn.disable();
								_store.baseParams = {
									'ORG_NO' : this.getTopToolbar().items.items[8]
											.getValue()
								}
								_store.load({
											params : {
												start : 0,
												limit : bbar.pageSize
											},
											callback : function() {
												btn.enable();
											}
										});
							},
							scope : this
						}];
				var _store = new Ext.data.JsonStore({
							// autoLoad:true,
							proxy : new Ext.data.HttpProxy({
										url : "getParameter.action",
										method : "GET"
									}),
							totalProperty : "totalCount",
							root : "data",
							fields : [{
										name : "PARAMETER_ID"
									}, {
										name : "PARAMETER_NAME"
									}, {
										name : "PARAMETER_VALUE"
									}, {
										name : "ORG_NAME"
									}, {
										name : "ORG_NO"
									}, {
										name : "REMARK"
									}]
						});
				var bbar = new Ext.lams.comp.PagingToolbar({
							store : _store
						});
				Ext.apply(this, {
							store : _store,
							sm : sm,
							cm : cm,
							tbar : tbar,
							bbar : bbar
						});
				this.addEvents('rowselect');
				this.addWin.on("submit", this.onAddWinSubmit, this);
				this.updateWin.on("submit", this.onUpdateWinSubmit, this);
				Ext.lams.ParameterManagerPanel.superclass.initComponent
						.call(this);
			},
			removeParameter : function() {
				try {
					var sel = this.getSelectedParameter();
					// this.getStore().remove(sel);
					var grid = this;
					Ext.Ajax.request({
								url : 'deleteParameter.action',
								method : 'POST',
								params : {
									'PARAMETER_ID' : sel.data['PARAMETER_ID']
								},
								success : function() {
									grid.getStore().remove(sel);
								}
							})
				} catch (error) {
					// Ext.Msg.alert("提示", error.message);
					H.Msg.error(error.message);
				}
			},
			onRemoveParameter : function(_btn) {
				if (_btn == 'yes')
					this.removeParameter();
			},
			getSelectedParameter : function() {
				var _sel = this.getSelectionModel();
				if (_sel.getCount() == 0)
					throw new Error("未选中某条记录");
				return _sel.getSelected();
			},
			addParameter : function(_r) {
				this.getStore().add(_r);
			},
			modifyParameter : function(_r) {
				try {
					var _sr = this.getSelectedParameter();
					var _data = _sr.data;
					for (var _i in _data) {
						_sr.set(_i, _r.get(_i));
					}
					_sr.commit();
					// this.fireEvent("rowselect", _sr);
				} catch (_err) {
					// Ext.Msg.alert("提示", _err.message);
					H.Msg.error(_err.message);
				}
			},
			onAddWinSubmit : function(_win, _r) {
				this.addParameter(_r);
			},
			onUpdateWinSubmit : function(_win, _r) {
				this.modifyParameter(_r);
			}
		});